package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.*;
import com.itheima.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TestSoulManager {
    @DubboReference
    QuestionnaireService questionnaireService;
    @DubboReference
    QuestionsService questionsService;
    @DubboReference
    OptionService optionService;
    @DubboReference
    UserlockService userlockService;
    @DubboReference
    TestSoulResultService testSoulResultService;
    @DubboReference
    TestSoulReportService testSoulReportService;
    @DubboReference
    UserInfoService userInfoService;

    public ResponseEntity testSoulList() {
        //获取所有问卷
        List<Questionnaire> questionnaires = questionnaireService.findAllQuestionnaire();
        //查询每个问卷下的问题以及选项,并封装到vo类中
        List<TestSoulListVo> testSoulListVos = new ArrayList<>();
        if (CollUtil.isNotEmpty(questionnaires)) {
            for (Questionnaire questionnaire : questionnaires) {
                TestSoulListVo testSoulListVo = new TestSoulListVo();
                testSoulListVo.setQuestionnaire(questionnaire);
                //查询每个问卷的问题
                List<Questions> questions = questionsService.findByNaireId(questionnaire.getId());

                //查询每个问题的选项并封装到vo类中
                List<TestSoulQuestionVo> questionVos = new ArrayList<>();
                for (Questions question : questions) {
                    TestSoulQuestionVo questionVo = new TestSoulQuestionVo();
                    List<Option> options = optionService.findByQuestionId(question.getId());

                    //封装选项到vo类中
                    List<TestSoulOptionVo> optionVos = new ArrayList<>();
                    for (Option option : options) {
                        TestSoulOptionVo optionVo = new TestSoulOptionVo();
                        optionVo.setId(String.valueOf(option.getId()));
                        optionVo.setOption(option.getOptions());
                        optionVos.add(optionVo);
                    }
                    questionVo.setId(String.valueOf(question.getId()));
                    questionVo.setQuestion(question.getQuestion());
                    questionVo.setOptions(optionVos);
                    questionVos.add(questionVo);
                }
                testSoulListVo.setQuestions(questionVos);
                //查询每个问卷是否上锁
                Userlock userlock = userlockService.findByUserAndNaire(UserHolder.get().getId(), questionnaire.getId());
                if (userlock != null) {
                    testSoulListVo.setIsLock(userlock.getIsLock());
                }
                //查询每个问卷是否有最后一次的报告
                TestSoulReport lastReport = testSoulReportService.findLastReport(UserHolder.get().getId(), questionnaire.getId());
                if (lastReport != null) {
                    testSoulListVo.setReportId(lastReport.getId().toString());
                }
                testSoulListVos.add(testSoulListVo);
            }
        }
        return ResponseEntity.ok(testSoulListVos);
    }

    public ResponseEntity submitSoulTest(List<Long> questions, List<Long> options) {
        //根据用户的选项查询用户最终得分
        Integer userScore = optionService.getUserScore(questions, options);
        //根据用户的得分去result表中查询对应的人格
        TestSoulResult result = testSoulResultService.findResultByScore(userScore);
        //查询数据库中是否已有用户的评测数据,有更新,没有保存
        Long reportId;
        Long userId = UserHolder.get().getId();
        //查找当前问卷id
        Questions question = questionsService.findNaire(questions.get(0));
        Long naireId = question.getNaireId();
        TestSoulReport testSoulReport = testSoulReportService.findByUserIdAndNaireId(userId, naireId);
        if (testSoulReport == null) {
            testSoulReport = new TestSoulReport();
            testSoulReport.setUserId(userId);
            testSoulReport.setResultId(result.getId());
            testSoulReport.setNaireId(naireId);
            reportId = Long.valueOf(testSoulReportService.save(testSoulReport));
        } else {
            reportId = testSoulReport.getId();
            testSoulReport.setResultId(result.getId());
            testSoulReportService.update(testSoulReport);
        }
        //根据用户当前填写的问卷决定是否解锁下一级问卷
        userlockService.lockDown(userId, naireId);
        return ResponseEntity.ok(reportId.toString());
    }

    public ResponseEntity findReport(Long reportId) {
        //查询报告
        TestSoulReport testSoulReport = testSoulReportService.findById(reportId);
        //查询结果
        TestSoulResult testSoulResult = testSoulResultService.findById(testSoulReport.getResultId());
        //封装鉴定结果和封面
        TestSoulReportVo testSoulReportVo = new TestSoulReportVo();
        testSoulReportVo.setConclusion(testSoulResult.getConclusion());
        testSoulReportVo.setCover(testSoulResult.getCover());
        //封装维度
        List<DimensionsVo> dimensions = new ArrayList<>();
        dimensions.add(new DimensionsVo("外向", testSoulResult.getExtroversionScore() + "%"));
        dimensions.add(new DimensionsVo("判断", testSoulResult.getJudgeScore() + "%"));
        dimensions.add(new DimensionsVo("抽象", testSoulResult.getAbstractScore() + "%"));
        dimensions.add(new DimensionsVo("理性", testSoulResult.getReasonScore() + "%"));
        testSoulReportVo.setDimensions(dimensions);
        //封装相似用户
        List<TestSoulReport> similars = testSoulReportService.findSimilar(testSoulReport.getNaireId(), UserHolder.get().getId(), testSoulResult.getId());
        List<SimilarYouVo> similarYouVos = new ArrayList<>();
        if (similars != null) {
            for (TestSoulReport similar : similars) {
                SimilarYouVo similarYouVo = new SimilarYouVo();
                //查询用户详情
                UserInfo userInfo = userInfoService.findById(similar.getUserId());
                similarYouVo.setId(userInfo.getId().intValue());
                similarYouVo.setAvatar(userInfo.getAvatar());
                similarYouVos.add(similarYouVo);
            }
        }
        testSoulReportVo.setSimilarYou(similarYouVos);
        return ResponseEntity.ok(testSoulReportVo);
    }
}
